package homework.Test5;
/*
字符串类型的数组

String str = "88,39,77,56,89,99,59,97";
存入到 int类型的数组
求int类型的数组中数据的最大值 最小值 求和 求平局值
定义方法求指定数据在数组中的索引 如果存在返回索引 不存在 返回-1

 */
public class Test {
    public static void main(String[] args) {
        String str = "88,39,77,56,89,99,59,97";
        String [] s1 = str.split(",");
        int [] arr = new int[s1.length];

        for (int i = 0; i < s1.length; i++) {
            arr[i] = Integer.valueOf(s1[i]);
        }

        int max = getMax(arr);
        int min = getMin(arr);
        int sum = getSum(arr);
        System.out.println(max);
        System.out.println(min);
        System.out.println(sum);
        System.out.println(sum/arr.length);

        int index = getIndex(arr,56);
        int index2 = getIndex(arr,90);
        System.out.println(index);
        System.out.println(index2);


    }

    public static int getMax(int[] arr){
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(max < arr[i]){
                max = arr[i];
            }
        }
        return max;
    }
    public static int getMin(int[] arr){
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(min > arr[i]){
                min = arr[i];
            }
        }
        return min;
    }
    public static int getSum(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static int getIndex(int[] arr,int num){
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if(num == arr[i]){
                index = i;
                return index;
            }
        }
        return index;
    }
}
